** Do file to generate the data used in Weisiger and Yarhi-Milo, "Revisiting Reputation: 
** How Past Actions Matter in International Politics".  Successfully running this do file
** will generate two files: repdata.dta and repdata_leaders.dta, that are used in the 
** analysis reported in the paper.

** The dataset in the analysis is constructed from existing sources.  We began with data
** produced by Eugene and then merged in variables that required prior processing from 
** a number of existing datasets.  The initial data (ddyadyr.out) is posted here; running
** this do file successfully will also require the following publicly available datasets:
** Militarized Interstate Disputes Data, version 3.1 (both dispute- and participant-level)
** International Crisis Behavior data, version 10
** The National Military Capabilities dataset, version 4.0
** The Archigos dataset of leaders, version 2.9 (leader-year version)
** The Alliance Treaty Obligations and Protocols (ATOP) dataset, version 3.0 (directed 
** dyad-year version)

** Generating the data is somewhat complex, so I provide a brief summary of the process 
** here.  We first process the MID and ICB datasets to create a country-year dataset that 
** contains information about any case in which a country recently backed down.  This 
** process entails generating a number of different variables that can be used to capture
** reputation based on past action.  While doing so, we also track several additional 
** relevant variables, such as the number of MIDs that a country has recently been 
** involved in.  We then merge these variables into the base dataset generated by Eugene 
** and generate a few additional variables for use in the main analysis.  At this point, 
** we have generated repdata.dta.  We then take a number of additional steps that are 
** necessary to merge in information about leaders from the Archigos dataset.  This 
** process produces repdata_leaders.dta, which we use in Table 2, Model 4.


clear all
set more off
set mem 3g

** I first need to get a version of the NMC dataset that I can merge in later on.
insheet using NMC_v4_0.csv, clear
save NMC, replace

** The first big step is to generate reputation variables from the MID datasets.

insheet using MIDA_3.10.csv, clear
save mida, replace

insheet using MIDB_3.10.csv, clear
merge m:1 dispnum using mida, keepus(outcome)
drop _merge

set obs 100000
gen cntry=.
gen ccode_alt=. /* will hold the country code of the primary country in the dispute in which the country lost reputation, for robustness purposes */
gen ccode_alt2=. /* same deal for countries that are coded as getting a good reputation */
gen yr=.
gen backdown=0
replace backdown=1 if sidea==1 & outcome==3
replace backdown=1 if sidea==0 & outcome==4
gen badoutcome=0
replace badoutcome=1 if sidea==1 & (outcome==2 | outcome==3)
replace badoutcome=1 if sidea==0 & (outcome==1 | outcome==4)

** I need to have one observation for every MID participant (over the ten-year span) so that I can 
** track MID involvement for robustness checks.  I also want to keep track of countries that fought 
** a war and lost.

gen lastmid=.
gen midsin10=.

** This loop creates a country-year observation for every country involved in a MID in the year the
** MID occurred and the subsequent nine years.  Note that this loop (like several others in this 
** do file) takes several minutes to run on a typical computer.

local i=1
levelsof dispnum, local(disputes)
foreach dispute of local disputes {
qui levelsof ccode if dispnum==`dispute', local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & ccode==`actor'
local year=r(max)
forvalues yr=0/9 {
qui count if cntry==`actor' & yr==`year'+`yr'
if r(N)==0 {
qui replace cntry=`actor' in `i'
qui replace yr=`year'+`yr' in `i'
local i=`i'+1
qui count if ccode==`actor' & endyear<`year'+`yr' & endyear>=`year'+`yr'-10
qui replace midsin10=r(N) if cntry==`actor' & yr==`year'+`yr'
qui sum endyear if ccode==`actor' & endyear<`year'+`yr'
if r(N)>0 {
qui replace lastmid=`year'+`yr'-r(max) if cntry==`actor' & yr==`year'+`yr'
}
else {
qui replace lastmid=-99 if cntry==`actor' & yr==`year'+`yr'
}
}
}
}
}

gen inmid10=1 /* All observations are for countries that were in a MID in the previous ten years */

** I'm going to generate a number of different variables to capture past backing down, based on 
** different scenarios.  I list those scenarios with the relevant variables below:
** backdown1: its side yields in a dispute, and the country did not use force (hostlev<4)
** backdown2: its side yields in a dispute
** backdown3: its side yields in a dispute, and the country did not escalate to war (hiact<20)
** backdown4: its side yields or loses in a dispute
** backdown5: its side yields or loses in a dispute, and the country did not escalate to war
** backdown6: backdown, but with a quicker decay function (5 years)
** backdown7: backdown, but with a slower decay function (20 years)

forvalues i=1/7 {
gen backdown`i'=.
}

** I also track backing down by dispute type (territorial, policy, and regime), using the primary
** definition of getting a bad reputation (backdown1 above).

foreach var in terr policy regime {
gen backdown_`var'1=.
}

** For robustness purposes, I'm going to track the strongest member of the opposing coalition who was 
** involved originally, so that I can drop dyads that involve that actor and the country that yielded (or won).
** This requires that I merge in data on state capabilities from the National Military Capabilities dataset.

rename endyear year
merge m:1 ccode year using NMC, keepus(cinc)
drop if _merge==2
drop _merge
rename year endyear

** The main loop below goes through all disputes in which at least one country backed down and 
** assigns the relevant values of the backdown variables.

levelsof dispnum if backdown==1, local(disputes)
foreach dispute of local disputes {
qui sum outcome if dispnum==`dispute'
local outcome=r(mean)
if `outcome'==3 {
qui sum cinc if dispnum==`dispute' & sidea==0 & orig==1
qui sum ccode if dispnum==`dispute' & sidea==0 & orig==1 & cinc==r(max)
local strongestenemy=r(mean)
qui levelsof ccode if dispnum==`dispute' & sidea==1, local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & sidea==1 & ccode==`actor'
local year=r(max)
qui sum hostlev if dispnum==`dispute' & sidea==1 & ccode==`actor'
local force=r(max)
qui sum hiact if dispnum==`dispute' & sidea==1 & ccode==`actor'
local hiact=r(max)
local terrrev=0
local regrev=0
local polrev=0
qui count if dispnum==`dispute' & sidea==1 & ccode==`actor' & (revtype1==1 | revtype2==1)
if r(N)>0 local terrrev=1
qui count if dispnum==`dispute' & sidea==1 & ccode==`actor' & (revtype1==2 | revtype2==2)
if r(N)>0 local polrev=1
qui count if dispnum==`dispute' & sidea==1 & ccode==`actor' & (revtype1==3 | revtype2==3)
if r(N)>0 local regrev=1
forvalues yr=0/19 {
if `yr'<10 {
qui replace ccode_alt=`strongestenemy' if cntry==`actor' & yr==`year'+`yr' & (backdown2==. | backdown2<1/`yr')
qui replace backdown2=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown2==. | backdown2<1-`yr'/10)
}
if `force'<4 {
qui replace backdown1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown1==. | backdown1<1-`yr'/10) & `yr'<10
qui replace backdown6=1-(`yr')/5 if cntry==`actor' & yr==`year'+`yr' & (backdown6==. | backdown6<1-`yr'/5) & `yr'<5
qui replace backdown7=1-(`yr')/20 if cntry==`actor' & yr==`year'+`yr' & (backdown7==. | backdown7<1-`yr'/20)
}
if `yr'<10 {
if `hiact'<20 {
qui replace backdown3=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown3==. | backdown3<1-`yr'/10)
}
*if `terrrev'==1 {
*qui replace backdown_terr1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_terr1==. | backdown_terr1<1-`yr'/10)
*}
if `terrrev'==1 & `force'<4 {
qui replace backdown_terr1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_terr1==. | backdown_terr1<1-`yr'/10)
}
*if `polrev'==1 {
*qui replace backdown_policy1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_policy1==. | backdown_policy1<1-`yr'/10)
*}
if `polrev'==1 & `force'<4 {
qui replace backdown_policy1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_policy1==. | backdown_policy1<1-`yr'/10)
}
*if `regrev'==1 {
*qui replace backdown_regime1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_regime1==. | backdown_regime1<1-`yr'/10)
*}
if `regrev'==1 & `force'<4 {
qui replace backdown_regime1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_regime1==. | backdown_regime1<1-`yr'/10)
}
}
}
}
}
if `outcome'==4 {
qui sum cinc if dispnum==`dispute' & sidea==1 & orig==1
qui sum ccode if dispnum==`dispute' & sidea==1 & orig==1 & cinc==r(max)
local strongestenemy=r(mean)
qui levelsof ccode if dispnum==`dispute' & sidea==0, local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & sidea==0 & ccode==`actor'
local year=r(max)
qui sum hostlev if dispnum==`dispute' & sidea==0 & ccode==`actor'
local force=r(max)
qui sum hiact if dispnum==`dispute' & sidea==0 & ccode==`actor'
local hiact=r(max)
local terrrev=0
local regrev=0
local polrev=0
qui count if dispnum==`dispute' & sidea==0 & ccode==`actor' & (revtype1==1 | revtype2==1)
if r(N)>0 local terrrev=1
qui count if dispnum==`dispute' & sidea==0 & ccode==`actor' & (revtype1==2 | revtype2==2)
if r(N)>0 local polrev=1
qui count if dispnum==`dispute' & sidea==0 & ccode==`actor' & (revtype1==3 | revtype2==3)
if r(N)>0 local regrev=1
forvalues yr=0/19 {
if `yr'<10 {
qui replace ccode_alt=`strongestenemy' if cntry==`actor' & yr==`year'+`yr' & (backdown2==. | backdown2<1-`yr'/10)
qui replace backdown2=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown2==. | backdown2<1-`yr'/10)
}
if `force'<4 {
qui replace backdown1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown1==. | backdown1<1-`yr'/10) & `yr'<10
qui replace backdown6=1-(`yr')/5 if cntry==`actor' & yr==`year'+`yr' & (backdown6==. | backdown6<1-`yr'/5) & `yr'<5
qui replace backdown7=1-(`yr')/20 if cntry==`actor' & yr==`year'+`yr' & (backdown7==. | backdown7<1-`yr'/20)
}
if `yr'<10 {
if `hiact'<20 {
qui replace backdown3=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown3==. | backdown3<1-`yr'/10)
}
*if `terrrev'==1 {
*qui replace backdown_terr1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_terr1==. | backdown_terr1<1-`yr'/10)
*}
if `terrrev'==1 & `force'<4 {
qui replace backdown_terr1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_terr1==. | backdown_terr1<1-`yr'/10)
}
*if `polrev'==1 {
*qui replace backdown_policy1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_policy1==. | backdown_policy1<1-`yr'/10)
*}
if `polrev'==1 & `force'<4 {
qui replace backdown_policy1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_policy1==. | backdown_policy1<1-`yr'/10)
}
*if `regrev'==1 {
*qui replace backdown_regime1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_regime1==. | backdown_regime1<1-`yr'/10)
*}
if `regrev'==1 & `force'<4 {
qui replace backdown_regime1=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown_regime1==. | backdown_regime1<1-`yr'/10)
}
}
}
}
}
}

** I use a separate loop for backdown4 and backdown5, which both involve getting a bad reputation for either yielding or losing
** in a dispute (unlike the standard approach of just examining yielding).

levelsof dispnum if badoutcome==1, local(disputes)
foreach dispute of local disputes {
qui sum outcome if dispnum==`dispute'
local outcome=r(mean)
if `outcome'==2 | `outcome'==3 {
qui levelsof ccode if dispnum==`dispute' & sidea==1, local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & sidea==1 & ccode==`actor'
local year=r(max)
qui sum hiact if dispnum==`dispute' & sidea==1 & ccode==`actor'
local hiact=r(max)
forvalues yr=0/9 {
qui replace backdown4=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown4==. | backdown4<1-`yr'/10)
if `hiact'<20 {
qui replace backdown5=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown5==. | backdown5<1-`yr'/10)
}
}
}
}
if `outcome'==1 | `outcome'==4 {
qui levelsof ccode if dispnum==`dispute' & sidea==0, local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & sidea==0 & ccode==`actor'
local year=r(max)
qui sum hiact if dispnum==`dispute' & sidea==0 & ccode==`actor'
local hiact=r(max)
forvalues yr=0/9 {
qui replace backdown4=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown4==. | backdown4<1-`yr'/10)
if `hiact'<20 {
qui replace backdown5=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (backdown5==. | backdown5<1-`yr'/10)
}
}
}
}
}


** Now I need a separate loop to generate a variable capturing good reputation (gained by fighting and winning).

gen foughtwon=0
replace foughtwon=1 if sidea==1 & outcome==1 & hiact>=20
replace foughtwon=1 if sidea==0 & outcome==2 & hiact>=20
gen foughtwon2=0
replace foughtwon2=1 if sidea==1 & (outcome==1 | outcome==4) & hiact>=20
replace foughtwon2=1 if sidea==0 & (outcome==2 | outcome==3) & hiact>=20

gen goodrep=.
gen goodrep2=.

levelsof dispnum if foughtwon==1, local(disputes)
foreach dispute of local disputes {
qui sum outcome if dispnum==`dispute'
local outcome=r(mean)
if `outcome'==1 {
qui sum cinc if dispnum==`dispute' & sidea==0 & orig==1
qui sum ccode if dispnum==`dispute' & sidea==0 & orig==1 & cinc==r(max)
local strongestenemy=r(mean)
qui levelsof ccode if dispnum==`dispute' & sidea==1, local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & sidea==1 & ccode==`actor'
local year=r(max)
forvalues yr=0/9 {
qui replace ccode_alt2=`strongestenemy' if cntry==`actor' & yr==`year'+`yr' & (goodrep==. | goodrep<1-`yr'/10)
qui replace goodrep=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (goodrep==. | goodrep<1-`yr'/10)
qui replace goodrep2=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (goodrep2==. | goodrep2<1-`yr'/10)
}
}
}
if `outcome'==2 {
qui sum cinc if dispnum==`dispute' & sidea==1 & orig==1
qui sum ccode if dispnum==`dispute' & sidea==1 & orig==1 & cinc==r(max)
local strongestenemy=r(mean)
qui levelsof ccode if dispnum==`dispute' & sidea==0, local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & sidea==0 & ccode==`actor'
local year=r(max)
forvalues yr=0/9 {
qui replace ccode_alt2=`strongestenemy' if cntry==`actor' & yr==`year'+`yr' & (goodrep==. | goodrep<1-`yr'/10)
qui replace goodrep=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (goodrep==. | goodrep<1-`yr'/10)
qui replace goodrep2=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (goodrep2==. | goodrep2<1-`yr'/10)
}
}
}
if `outcome'==3 {
qui levelsof ccode if dispnum==`dispute' & sidea==1, local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & sidea==1 & ccode==`actor'
local year=r(max)
forvalues yr=0/9 {
qui replace goodrep2=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (goodrep2==. | goodrep2<1-`yr'/10)
}
}
}
if `outcome'==4 {
qui levelsof ccode if dispnum==`dispute' & sidea==0, local(actors)
foreach actor of local actors {
qui sum endyear if dispnum==`dispute' & sidea==0 & ccode==`actor'
local year=r(max)
forvalues yr=0/9 {
qui replace goodrep2=1-(`yr')/10 if cntry==`actor' & yr==`year'+`yr' & (goodrep2==. | goodrep2<1-`yr'/10)
}
}
}
}

** I also generate variables that take into account all MIDs in which the country recently has been involved.  
** These are badallm (proportion of all past MIDs in which the country acted in a way that would give it a 
** bad reputation), goodallm (proportion of all past MIDs in which the country acted in a way that would give
** it a good reputation), and genrep (taking into account both good and bad past action: AltRep_it in the paper).
** Generating these variables requires that I have a count of all MIDs in which a country is involved in a year, 
** sorted by outcome (yield, fight and win, or other).

gen yields=0
gen fightwin=0
gen other=0

qui count if ccode!=.
local i=r(N)+1
levelsof dispnum, local(disputes)
foreach dispute of local disputes {
qui levelsof ccode if dispnum==`dispute', local(ccodes)
qui sum outcome if dispnum==`dispute'
local outcome=r(mean)
foreach ccode of local ccodes {
qui sum endyear if ccode==`ccode' & dispnum==`dispute'
local year=r(max)
qui count if cntry==`ccode' & yr==`year'
if r(N)==0 {
qui replace cntry=`ccode' in `i'
qui replace yr=`year' in `i'
local i=`i'+1
}
qui count if sidea==0 & ccode==`ccode' & dispnum==`dispute'
if r(N)>0 local sideb=1
else local sideb=0
qui count if sidea==1 & ccode==`ccode' & dispnum==`dispute'
if r(N)>0 local sidea=1
else local sidea=0
if `sidea'==1 & `sideb'==1 local doubles=`doubles'+1
if `sidea'==1 & `sideb'==0 {
if `outcome'==3 qui replace yields=yields+1 if cntry==`ccode' & yr==`year'
qui sum hostlev if dispnum==`dispute' & ccode==`ccode'
local hostlev=r(max)
if `outcome'==1 & `hostlev'>=4 qui replace fightwin=fightwin+1 if cntry==`ccode' & yr==`year'
if `outcome'!=3 & (`outcome'!=1 | `hostlev'<4) qui replace other=other+1 if cntry==`ccode' & yr==`year'
}
if `sidea'==0 & `sideb'==1 {
if `outcome'==4 qui replace yields=yields+1 if cntry==`ccode' & yr==`year'
qui sum hostlev if dispnum==`dispute' & ccode==`ccode'
local hostlev=r(max)
if `outcome'==2 & `hostlev'>=4 qui replace fightwin=fightwin+1 if cntry==`ccode' & yr==`year'
if `outcome'!=4 & (`outcome'!=1 | `hostlev'<4) qui replace other=other+1 if cntry==`ccode' & yr==`year'
}
}
}

drop backdown badoutcome foughtwon
rename yr year
rename ccode country
rename cntry ccode

drop if ccode==.

save midrepdata, replace

keep ccode year yields fightwin other

gen genrep=.
gen goodallm=.
gen badallm=.
levelsof ccode, local(ccodes)
foreach ccode of local ccodes {
di `ccode'
qui sum year if ccode==`ccode'
local yearmin=r(min)
local yearmax=r(max)
if `yearmax'>2001 local yearmax=2001
forvalues year=`yearmin'/`yearmax' {
local yields=0
local fightwin=0
local other=0
local allmids=0
forvalues yr=1/10 {
qui sum yields if ccode==`ccode' & year==`year'-`yr'
if r(N)>0 {
local yields=`yields'+r(mean)-r(mean)*(`yr'-1)/10
local allmids=`allmids'+r(mean)-r(mean)*(`yr'-1)/10
}
qui sum fightwin if ccode==`ccode' & year==`year'-`yr'
if r(N)>0 {
local fightwin=`fightwin'+r(mean)-r(mean)*(`yr'-1)/10
local allmids=`allmids'+r(mean)-r(mean)*(`yr'-1)/10
}
qui sum other if ccode==`ccode' & year==`year'-`yr'
if r(N)>0 {
local other=`other'+r(mean)-r(mean)*(`yr'-1)/10
local allmids=`allmids'+r(mean)-r(mean)*(`yr'-1)/10
}
qui replace genrep=(`yields'-`fightwin')/`allmids' if ccode==`ccode' & year==`year'
qui replace goodallm=`fightwin'/`allmids' if ccode==`ccode' & year==`year'
qui replace badallm=`yields'/`allmids' if ccode==`ccode' & year==`year'
}
}
}

** Finally, I need to shift years by one to match the lag introduced elsewhere (otherwise the reputational
** effect doesn't hit until two years after the incident in the data)

replace year=year-1

save altbackdown, replace


** Table 1, Model 4 uses ICB data instead of MID data to generate the reputation variable.  This code generates
** the relevant variable, following a similar procedure to that used above.

insheet using icb2v10.txt, clear

** First thing is to drop the intrawar crises, which are inappropriate for this purpose

gen crisiwc=0
levelsof crisno, local(crises)
foreach crisis of local crises {
qui count if iwc>1 & crisno==`crisis'
if r(N)>0 qui replace crisiwc=1 if crisno==`crisis'
}

drop if crisiwc==1

** I also drop a few observations involving countries that appear multiple times within a single crisis -- this
** approach avoids double counting their experiences later.

drop if crisno==21 & (monres==10 | monres==11) /* Second appearance for Hungary, Czechoslovakia, and Yugoslavia in a 1921 crisis related to the Hungarian monarchy */
drop if crisno==48 & cracid==355 & monres==3 /* Bulgaria's first appearance in a Bulgaria-Turkey crisis in 1935 */
drop if crisno==80 & cracid==360 & monres!=6 /* Romania's second two appearances in a crisis relating to a series of demands by its neighbors in 1940 */
drop if crisno==146 & cracid==713 & outcom==5 /* Taiwan's second appearance in the Taiwan Straits crisis */
drop if crisno==176 & cracid==490 & monres==8 /* Congo's second appearance in a Belgium-Congo crisis in 1960 */
drop if crisno==294 & cracid==93 & outcom==1 /* Nicaragua's first appearance in a crisis with Costa Rica in 1978-9 that ultimately ended with the Nicaraguan president going into exile */

** Now to create the space for country-year reputation observations.

set obs 10000
gen ccode=.
gen yr=.
forvalues i=1/3 {
gen bkdown`i'=.
}
forvalues i=1/3 {
forvalues j=1/5 {
gen bkdown`i'`j'=.
}
}

** next thing to do is to create observations for every crisis participant in the year of crisis resolution and the nine 
** subsequent years

** Note that I will be using a lagged dependent variable in the analysis, so I want the first year in which a country
** is coded as getting a bad reputation to be the one in which it backs down (as the MID will show up in the previous 
** year)

local i=1
levelsof crisno, local(crises)
foreach crisis of local crises {
qui levelsof cracid if crisno==`crisis', local(actors)
foreach actor of local actors {
qui sum yerres if crisno==`crisis' & cracid==`actor'
local year=r(mean)
forvalues j=0/9 {
qui count if ccode==`actor' & yr==`year'+`j'
if r(N)==0 {
qui replace ccode=`actor' in `i'
qui replace yr=`year'+`j' in `i'
local i=`i'+1
}
}
}
}

gen incrisis=1

drop if ccode==.

** Next step is to track whether a country backed down in a crisis.  I operationalize backing down in several ways:
** 1) a country is coded as losing the crisis (i.e. outcom==4) and it did not use violence (majres<8)
** 2) a country is coded as losing the crisis
** 3) a country is coded as losing the crisis, plus the crisis did not end up in war (sevvio<4)

levelsof crisno, local(crises)
foreach crisis of local crises {
qui levelsof cracid if crisno==`crisis', local(actors)
foreach actor of local actors {
qui sum outcom if crisno==`crisis' & cracid==`actor'
local outcome=r(mean)
qui sum majres if crisno==`crisis' & cracid==`actor'
local response=r(mean)
qui sum sevvio if crisno==`crisis' & cracid==`actor'
local warlev=r(mean)
qui sum yerres if crisno==`crisis' & cracid==`actor'
local year=r(mean)
if `outcome'==4 & `response'<8 { /* Defeat in crisis without violence */
forvalues yr=0/9 {
qui replace bkdown1=1-(`yr')/10 if ccode==`actor' & yr==`year'+`yr' & (bkdown1==. | bkdown1<1/`yr')
}
}
if `outcome'==4 { /* Defeat in crisis */
forvalues yr=0/9 {
qui replace bkdown2=1-(`yr')/10 if ccode==`actor' & yr==`year'+`yr' & (bkdown2==. | bkdown2<1/`yr')
}
}
if `outcome'==4 & `warlev'<4 { /* Defeat in crisis without war */
forvalues yr=0/9 {
qui replace bkdown3=1-(`yr')/10 if ccode==`actor' & yr==`year'+`yr' & (bkdown3==. | bkdown3<1/`yr')
}
}
}
}

keep ccode yr bkdown* incrisis
rename yr year

save icbrepdata, replace

** I also have to create a dyad-year ID variable in the ATOP data to permit merging later on.

use atop3_0ddyr, clear
gen long dyadidyr=ddyad*1000+year-1000
replace dyadidyr=ddyad*1000+year-2000 if year>=2000
save atop, replace



** Now that the reputation variables have been generated, it's time to bring them into the main dataset.

insheet using ddyadyear.out, clear

drop milper* milex* irst* energy* tpop* upop* majpow* rlregion region* col* distance polity1 polity2 mzongo* mzinitnm mzhost* mzrevis* mzkeynum mzorig* mzjo* mzmidnme mzfatex* mzhiact* mzoutcm mzsettle mzrecip mznumst* mzcowwar mznmmd* mzrole* mzpceyrs democ* autoc* depend* imputed durable*

lab var ccode1 "COW country code, side 1"
lab var ccode2 "COW country code, side 2"
lab var year "Year"
lab var cap_1 "CINC score for ccode1"
lab var cap_2 "CINC score for ccode2"
lab var pol_rel "Politically relevant dyad"
lab var contig "COW contiguity score"
lab var polity21 "Polity score for ccode1"
lab var polity22 "Polity score for ccode2"

rename mzinit mzmid
lab var mzmid "MID initiation"


drop if year>2001 /* extent of MID data */

gen long dyadidyr=ccode1*1000000+ccode2*1000+year-1000
replace dyadidyr=((ccode1*1000000)+(ccode2*1000)+(year-2000)) if year>=2000
lab var dyadidyr "dyad-year ID variable"

gen dyadid=ccode1*1000+ccode2
lab var dyadid "dyad ID variable"

sort dyadidyr

** generating leading dependent variable

sort ccode1 ccode2 year

gen mzmidl=mzmid[_n+1] if ccode1==ccode1[_n+1] & ccode2==ccode2[_n+1]
lab var mzmidl "MID onset (lead 1 year)"

gen mzfataldl=mzfatald[_n+1] if ccode1==ccode1[_n+1] & ccode2==ccode2[_n+1]
gen mzfatall=0 if mzmidl~=.
replace mzfatall=1 if mzmidl==1 & mzfataldl~=0 & mzfataldl~=.
lab var mzfatall "Fatal MID onset (lead 1 year)"

replace polity21=. if polity21<-10
replace polity22=. if polity22<-10

gen demlo=polity21
replace demlo=polity22 if polity22<polity21 & polity21!=.
lab var demlo "Lower dyadic democracy score"

gen cntgdumy=0
replace cntgdumy=1 if contig<=4
lab var cntgdumy "Contiguity"

gen capsum=cap_1+cap_2
gen capratio=cap_1/(cap_1+cap_2)
replace capratio=cap_2/(cap_1+cap_2) if cap_2>cap_1
lab var capsum "Sum of CINC scores"
lab var capratio "Capability Ratio"

** Major power status
gen majpowra=0
gen majpowrb=0
replace majpowra=1 if ccode1==2 & year>=1989
replace majpowrb=1 if ccode2==2 & year>=1989
replace majpowra=1 if ccode1==200
replace majpowrb=1 if ccode2==200
replace majpowra=1 if ccode1==220 & year<=1940
replace majpowrb=1 if ccode2==220 & year<=1940
replace majpowra=1 if ccode1==220 & year>1945
replace majpowrb=1 if ccode2==220 & year>1945
replace majpowra=1 if ccode1==255 & year<=1918
replace majpowrb=1 if ccode2==255 & year<=1918
replace majpowra=1 if ccode1==255 & year>=1925 & year<=1945
replace majpowrb=1 if ccode2==255 & year>=1925 & year<=1945
replace majpowra=1 if ccode1==255 & year>=1991
replace majpowrb=1 if ccode2==255 & year>=1991
replace majpowra=1 if ccode1==300 & year<=1918
replace majpowrb=1 if ccode2==300 & year<=1918
replace majpowra=1 if ccode1==325 & year>=1860 & year<=1943
replace majpowrb=1 if ccode2==325 & year>=1860 & year<=1943
replace majpowra=1 if ccode1==365 & year<=1917
replace majpowrb=1 if ccode2==365 & year<=1917
replace majpowra=1 if ccode1==365 & year>=1922
replace majpowrb=1 if ccode2==365 & year>=1922
replace majpowra=1 if ccode1==710 & year>=1950
replace majpowrb=1 if ccode2==710 & year>=1950
replace majpowra=1 if ccode1==740 & year>=1895 & year<=1945
replace majpowrb=1 if ccode2==740 & year>=1895 & year<=1945
replace majpowra=1 if ccode1==740 & year>=1991
replace majpowrb=1 if ccode2==740 & year>=1991

gen bothmajr=0
replace bothmajr=1 if majpowra==1 & majpowrb==1
gen onemajor=0
replace onemajor=1 if majpowra==1 | majpowrb==1
lab var bothmajr "Both major powers"
lab var onemajor "One major power"

** Now to merge in the ATOP data

merge 1:1 dyadidyr using atop, keepus(atopally)
tab _merge
drop if _merge==2 /* mostly post-2001, also a few non-COW system members */
drop _merge
replace atopally=0 if atopally==.
lab var atopally "Dyadic alliance"

** Now to bring in reputation data

rename ccode2 ccode

merge m:1 ccode year using midrepdata, keepus(backdown* inmid10 midsin10 ccode_alt goodrep*)
tab _merge
drop if _merge==2 /* observations after 2002 or countries that exited the international system */
forvalues i=1/7 {
replace backdown`i'=0 if backdown`i'==.
}
replace midsin10=0 if midsin10==.
foreach var in terr policy regime {
replace backdown_`var'1=0 if backdown_`var'1==.
}
drop _merge
replace goodrep=0 if goodrep==.
replace goodrep2=0 if goodrep2==.
merge m:1 ccode year using altbackdown, keepus(genrep goodallm badallm)
drop if _merge==2
foreach var in genrep goodallm badallm {
qui replace `var'=0 if `var'==.
}
drop _merge

gen backdown_nonterr1=backdown1-backdown_terr1 /* captures observations in which a country backed down on something unrelated to territory */

merge m:1 ccode year using icbrepdata, keepus(bkdown1 bkdown2 bkdown3 incrisis)
tab _merge
drop if _merge==2 /* observations after 2002 or countries that exited the international system */
forvalues i=1/3 {
replace bkdown`i'=0 if bkdown`i'==.
}
drop _merge

rename ccode ccode2

lab var ccode_alt "ccode of country ccode2 yielded to"
lab var midsin10 "Count of MIDs involving ccode2 in prior 10 years"
lab var inmid10 "Dummy: 1 = ccode2 in 1+ MIDs in prior 10 years"
lab var backdown1 "Bad reputation: main variable"
lab var backdown2 "Bad reputation: any yielding (even after force/war)"
lab var backdown3 "Bad reputation: no war"
lab var backdown4 "Bad reputation: any MID yield or loss"
lab var backdown5 "Bad reputation: any MID yield or loss prior to war"
lab var backdown6 "backdown1 with quicker reputational decay"
lab var backdown7 "backdown1 with slower reputational decay"
lab var backdown_terr1 "backdown1, only for territorial MIDs"
lab var backdown_policy1 "backdown1, only for policy MIDs"
lab var backdown_regime1 "backdown1, only for regime MIDs"
lab var goodrep "Good reputation: main variable"
lab var goodrep2 "Good reputation, including victory without fighting"
lab var genrep "AltRep_it: Good and bad reputation averaged over recent MIDs"
lab var goodallm "Good reputation, averaged over all recent MIDs"
lab var badallm "Bad reputation, average over all recent MIDs"
lab var backdown_nonterr1 "Bad reputation from non-territorial MIDs"
lab var bkdown1 "Bad reputation: main ICB coding"
lab var bkdown2 "Bad reputation: any ICB crisis loss"
lab var bkdown3 "Bad reputation: ICB crisis loss without war"
lab var incrisis "ccode2 was in a crisis in the previous 10 years"


** now to generate the issue-specific dispute variables

sort ccode1 ccode2 year
gen terrmidl=0
replace terrmidl=1 if mzmidl==1 & ccode1==ccode1[_n+1] & ccode2==ccode2[_n+1] & year==year[_n+1]-1 & (mzrevt11[_n+1]==1 | mzrevt12[_n+1]==1 | mzrevt21[_n+1]==1 | mzrevt22[_n+1]==1)
replace terrmidl=. if mzmidl==.
gen polmidl=0
replace polmidl=1 if mzmidl==1 & ccode1==ccode1[_n+1] & ccode2==ccode2[_n+1] & year==year[_n+1]-1 & (mzrevt11[_n+1]==2 | mzrevt12[_n+1]==2 | mzrevt21[_n+1]==2 | mzrevt22[_n+1]==2)
replace polmidl=. if mzmidl==.
gen regmidl=0
replace regmidl=1 if mzmidl==1 & ccode1==ccode1[_n+1] & ccode2==ccode2[_n+1] & year==year[_n+1]-1 & (mzrevt11[_n+1]==3 | mzrevt12[_n+1]==3 | mzrevt21[_n+1]==3 | mzrevt22[_n+1]==3)
replace regmidl=. if mzmidl==.
lab var terrmidl "Territorial MID onset (leading)"
lab var polmidl "Policy MID onset (leading)"
lab var regmidl "Regime MID onset (leading)"

sort ccode1 ccode2 year
gen t=1 if ccode1!=ccode1[_n-1] | ccode2!=ccode2[_n-1] | year!=year[_n-1]+1 | mzmidl[_n-1]==1
replace t=t[_n-1]+1 if t==.
gen t2=t^2
gen t3=t^3

gen tterr=1 if ccode1!=ccode1[_n-1] | ccode2!=ccode2[_n-1] | year!=year[_n-1]+1 | terrmidl[_n-1]==1
replace tterr=tterr[_n-1]+1 if tterr==.
gen tterr2=tterr^2
gen tterr3=tterr^3

drop cap_1 cap_2 

compress
sort dyadidyr

save repdata, replace


** Now I generate the data for the leaders analysis.  While conceptually straightforward, actually 
** conducting the merge is complicated given years in which multiple leaders held power in a single
** country.

use "Archigos_v.2.9_tv-Public.dta", clear
gen cntry=.
gen yr=.
gen obs=.

local i=1
levelsof ccode, local(countries)
foreach country of local countries {
qui sum year if ccode==`country'
local yearmin=r(min)
local yearmax=r(max)
forvalues yr=`yearmin'/`yearmax' {
qui count if ccode==`country' & year==`yr'
if r(N)>0 {
qui replace obs=r(N) in `i'
qui replace cntry=`country' in `i'
qui replace yr=`yr' in `i'
local i=`i'+1
}
}
}

sort ccode year
gen yrleader=1
replace yrleader=yrleader[_n-1]+1 if ccode==ccode[_n-1] & year==year[_n-1]
gen yrpct=(endobs-startobs+1)/365
replace yrpct=(endobs-startobs+1)/366 if year(endobs)/4==round(year(endobs)/4) /* accounting for leap years */
sort cntry yr

save archigos_merge, replace

use repdata, clear

merge 1:1 _n using archigos_merge, keepus(cntry yr obs)
drop _merge

gen yrleader=1
gen n=_n
gen origobs=1

count if cntry!=.
local obs=r(N)
count
local totobs=r(N)
forvalues n=1/`obs' {
if `n'/1000==round(`n'/1000) di `n'
if obs[`n']>1 {
local i=`totobs'+1
qui count if ccode2==cntry[`n'] & year==yr[`n']
local yrobs=r(N)
local totobs=`totobs'+`yrobs'*(obs[`n']-1)
qui set obs `totobs'
qui replace ccode2=cntry[`n'] if ccode2==.
qui replace year=yr[`n'] if year==.
qui levelsof ccode1 if ccode2==cntry[`n'] & year==yr[`n'], local(ccodes)
foreach ccode of local ccodes {
local rounds=obs[`n']-1
forvalues z=1/`rounds' {
qui replace ccode1=`ccode' in `i'
local i=`i'+1
}
}
}
}

replace origobs=0 if origobs==.
sort ccode1 ccode2 year yrleader

foreach var in mzmid midsin10 backdown1 backdown2 backdown3 backdown4 backdown5 caprat capsum demlo atopally cntgdumy bothmajr onemajor t t2 t3 dyadid {
qui replace `var'=`var'[_n-1] if `var'==. & origobs==0
}

replace yrleader=yrleader[_n-1]+1 if yrleader==.

rename ccode2 ccode

merge m:1 ccode year yrleader using archigos_merge, keepus(startobs endobs leadid leader entry exit eindate yrpct)

drop if _merge==2 /* post-2001 plus Archigos observations for years prior to COW system entry */

rename ccode ccode2

** I have to do two things: generate versions of the reputation variable that 
** revert to zero when a new leader enters office, and match MIDs to leaders.  I 
** start with the latter.

** I have multiple records per year in cases in which there is more than one
** leader in a given year for a given country.  When there also occurs a MID in
** the year of transition, I need to recode the MID onset variable to zero for
** leaders who did not face a MID during their time in office.

gen mzleadmid=mzmid
replace mzleadmid=0 if mzstmo2<month(startobs)
replace mzleadmid=0 if mzstmo2==month(startobs) & mzstday2<day(startobs)
replace mzleadmid=0 if mzstmo2>month(endobs) & mzstmo2!=.
replace mzleadmid=0 if mzendmo2==month(endobs) & mzenddy2>day(endobs)

sort ccode1 ccode2 leadid year
gen mzleadmidl=mzleadmid[_n+1] if ccode1==ccode1[_n+1] & ccode2==ccode2[_n+1] & leadid==leadid[_n+1] & year==year[_n+1]-1

** Now to update the reputation variable -- it reverts to zero whenever a new
** leader comes into power

forvalues i=1/5 {
gen backdown`i'_leader=backdown`i'
replace backdown`i'_leader=0 if year-year(eindate)<11-10*backdown`i'
replace backdown`i'_leader=. if _merge==1 /* observations not covered by Archigos */
gen backdown`i'_newleader=backdown`i'-backdown`i'_leader
}

drop _merge

save repdata_leaders, replace

